﻿<template>
    <div class="app-container">
        <!-- 角色搜索区-->
        <el-form :model="queryParams" ref="queryForm"  :inline="true">
            <el-form-item label="角色名称" class="item">
                <el-input
                        v-model="queryParams.roleName"
                        placeholder="请输入角色名称"
                        clearable
                        size="small"
                        style="width: 240px"
                        @keyup.enter.native="load"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="load">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="reset" >重置</el-button>
            </el-form-item>
        </el-form>

        <!--按钮区域    -->
        <el-row :gutter="10" class="mb8"   style="margin:5px">
            <el-button type="primary" plain  icon="el-icon-plus"  size="mini" >新增</el-button>
            <el-button type="success" plain icon="el-icon-edit" size="mini"   >修改</el-button>
        </el-row>

        <!-- 角色列表区-->
        <el-table :data="tableData" :header-cell-style="{background:'#F8F8F9'}"  style="width: 100%">
            <el-table-column type="index"  :index="indexMethod" label="编号" width="100px"></el-table-column>
            <el-table-column prop="roleName" label="角色名称"></el-table-column>
            <el-table-column prop="roleCode" label="权限字符"></el-table-column>
            <el-table-column label="状态" align="center">
                <template slot-scope="scope">
                    <el-switch
                            v-model="scope.row.status"
                            active-value="0"
                            inactive-value="1"
                            @change="handleStatusChange(scope.row)"
                    ></el-switch>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="260">
                <template #default="scope">
                    <button type="button"  class="el-button el-button--text el-button--mini"><!----><i class="el-icon-edit"></i><span>修改</span></button>
                    <button type="button"   class="el-button el-button--text el-button--mini"><!----><i class="el-icon-delete"></i><span style="color:red">删除</span></button>
                </template>
            </el-table-column>
        </el-table>


        <!--角色列表分页-->
        <div style="margin: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"> //双引号里面的total是定义一个变量，在这里定义了，就需要在下面data里面初始化一下
            </el-pagination>

        </div>

    </div>
</template>

<script>
    import request from "../utils/request";

    export default {
        name: "Role",
        data() {
            return {
                title: '',
                dialogVisible: false,
                // 查询参数
                queryParams: {
                    roleName: '',
                },
                currentPage: 1,
                pageSize: 5,
                total: 0,  //默认10条
                tableData: [],

            }
        },
        //页面加载完立即执行的方法
        created() {
            this.load()
        },

        methods: {

            indexMethod(index) {
                return index + 1;
            },


            //查询方法
            load() {
                request.get("/role/selectList", {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        username: this.queryParams.username
                    }
                }).then(res => {
                    this.tableData = res.data.list
                    this.total = res.data.total
                }).catch(error => error)

            },


            // 角色状态修改
            handleStatusChange(row) {
                let text = row.status === "0" ? "启用" : "停用";
                this.$confirm('确认要"' + text + '""' + row.roleName + '"角色吗？','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    request.get("/role/changeRoleStatus" ).then(res => {
                        if (res.code === '0') {
                            this.$message({
                                type: "success",
                                message: res.msg
                            })
                        }
                        this.load()
                    })
                }).catch(function() {
                    row.status = row.status === "0" ? "1" : "0";
                });
            },


            //改变当前每页条数触发
            handleSizeChange(pageSize) {
                this.pageSize = pageSize
                this.load()
            },

            //改变当前页码触发
            handleCurrentChange(pageNum) {
                this.currentPage = pageNum
                this.load()
            },
        }

    }
</script>

<style scoped>

</style>
